package com.cskaoyan.controller.admin.promotion;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.promotion.bo.AdBo;
import com.cskaoyan.bean.admin.promotion.vo.AdListVo;
import com.cskaoyan.bean.admin.promotion.vo.ItemsBean;
import com.cskaoyan.service.admin.PromotionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Name : TopicController.java
 * @Time : 2021/8/12 19:36
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@RestController
@RequestMapping("admin/ad")
public class AdController {

    @Autowired
    PromotionService promotionService;

    //得到所有广告
    @RequiresPermissions(value = {"*", "admin:ad:list"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo getList(String name, String content, BaseParamBo param){
        AdListVo adList = promotionService.getAdList(name, content, param);
        return BaseRespVo.ok(adList);
    }

    @RequiresPermissions(value = {"*", "admin:ad:update"}, logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo updateAd(@RequestBody AdBo adBo){
        ItemsBean itemsBean = promotionService.updateAd(adBo);
        if(itemsBean==null){
            return BaseRespVo.fail("没有更新内容~");
        }else if("违规".equals(itemsBean.getContent())){
            return BaseRespVo.fail("广告内容有违规信息！");
        }
        return BaseRespVo.ok(itemsBean);
    }

    @RequiresPermissions(value = {"*", "admin:ad:delete"}, logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo deleteAd(@RequestBody AdBo adBo){
        promotionService.deleteAd(adBo);
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = {"*", "admin:ad:create"}, logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo createAd(@RequestBody AdBo adBo){
        ItemsBean itemsBean = promotionService.createAd(adBo);
        return BaseRespVo.ok(itemsBean);
    }
}
